import Vue from 'vue'
import axios from 'axios'
// import store from '@/store'
// import { notification } from 'ant-design-vue/es/notification'
// import { message } from 'ant-design-vue'
import { VueAxios } from './axios'
// import { ACCESS_TOKEN } from '@/store/mutation-types'

// 创建 axios 实例
const service = axios.create({
  // baseURL: process.env.VUE_APP_REQUEST_URL, // api base_url
  timeout: 60000 // 请求超时时间
})
const err = (error) => {
  if (error.response) {
    alert(error.response.data.data, 3)
    // message.error(error.response.data.data, 3)
    
    const data = error.response.data
    // const token = Vue.ls.get(ACCESS_TOKEN)
    // if (error.response.status === 403) {
    //   notification.error({
    //     message: 'Forbidden',
    //     description: data.message
    //   })
    // }
    if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
      // notification.error({
      //   message: 'Unauthorized',
      //   description: 'Authorization verification failed'
      // })
      // if (token) {
      //   store.dispatch('Logout').then(() => {
      //     setTimeout(() => {
      //       window.location.reload()
      //     }, 1500)
      //   })
      // }
    }
  }
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(config => {
  // const token = Vue.ls.get(ACCESS_TOKEN)
  // if (token) {
  //   config.headers['Authorization'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  // }
  // config.headers['platform_code'] = process.env.VUE_APP_PLATFORM_CODE
  // config.headers['api-version'] = 'v1'
  return config
}, err)

// response interceptor
service.interceptors.response.use((response) => {
  if (response.config.responseType === 'blob') {
    return response
  }
  return response.data
}, err)

const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, service)
  }
}
const blobAjax = (rUrl, method, callback, data, option) => {
  const x = new XMLHttpRequest()
  // x.open(method, process.env.VUE_APP_REQUEST_URL + rUrl, true)
  x.open(method, rUrl, true)
  x.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
  x.responseType = 'blob'
  // if (Vue.ls.get(ACCESS_TOKEN)) {
  //   x.setRequestHeader('Authorization', 'bearer ' + Vue.ls.get(ACCESS_TOKEN))
  // }
  x.onload = function (e) {
    option.progress && option.progress(100)
    callback(e)
  }
  x.onloadstart = function (event) {
    option.progress && option.progress(30)
    option.start && option.start(event)
  }
  x.onloadEnd = event => {
    option.end && option.end(event)
  }
  x.onerror = event => {
    option.error && option.error(event)
  }
  x.onprogress = function (event) {
    if (event.lengthComputable) {
      option.progress && option.progress((event.loaded / event.total) * 100)
    }
  }
  x.upload.onprogress = function (event) {
    if (event.lengthComputable) {
      option.progress && option.progress((event.loaded / event.total) * 100)
    }
  }
  if (data) {
    x.send(JSON.stringify(data))
  } else {
    x.send()
  }
}

export {
  installer as VueAxios,
  service as axios,
  blobAjax
}
